from Tkinter import *
import Tkinter
import tkMessageBox

import jarvars
from save import saveConfiguration

class addRoom(Frame):

    def __init__(self, master=None):
                    
        self.parent = master
        self.win = Toplevel(master)
        self.win.geometry("%dx%d%+d%+d" % (400, 100, 150, 0))
        Frame.__init__(self,self.win)
        self.room = StringVar()

        self.grid(padx=2, pady=2,sticky=N+S+E+W)


        self.roomToAdd = Entry(self, width=15)
        self.roomToAddLabel = Label(self, text="Room to add")
        self.addButton = Button(self, text="Add", command=self.addRoom)
        


        self.roomToAddLabel.grid(row=0, column=0, sticky = E)
        self.roomToAdd.grid(row=0, column=1, sticky=W)
        self.addButton.grid(row=0,column=2, sticky=W)
        self.bind("<Destroy>", self._destroy)
        self.master.title("Add Room")
        
       
    def addRoom(self):
        
        room = self.roomToAdd.get()
        if len(room) == 0:
            return

        for ii in jarvars.Lights:
            if ii[0] == room:
                print "Room already exists"
                return
        jarvars.Lights.append([room,[]])
        jarvars.State.append([room,[]])
        self.roomToAdd.delete(0,END)
        self.parent.updateStateList()
                
    def _destroy(self, Event=None):
        saveConfiguration()
        self.win.destroy()
    
if __name__ == "__main__":
    print "Not a main module"
    root = Tk()
    eu = addLight(root)
    while(1):
        eu.update()
    
    


